package com.vidmt.api.modules.sys.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

import com.vidmt.api.modules.sys.Enums.PayType;

public class UserStatUtil {
	private static Queue<RegItem> last10Stat = new ArrayBlockingQueue<>(10);
	private static HourInfo[] hourinfos;
	private static int today = -1;
	static {
		hourinfos = new HourInfo[48];
		for (int i = 0; i < hourinfos.length; i++) {
			hourinfos[i] = new HourInfo();
		}
	}

	private static class RegItem {
		public Long uid;
		public String nick;
		public Date regtime;

		public RegItem(Long uid, String nick, Date regtime) {
			this.uid = uid;
			this.nick = nick;
			this.regtime = regtime;
		}
	}

	private static class HourInfo {
		public int aRegcnt;
		public int aPaycnt;
		public int aMoneysum;

		public int iRegcnt;
		public int iPaycnt;
		public int iMoneysum;
	}

	private static void initHourinfo(Calendar cld) {
		int date = cld.get(Calendar.DAY_OF_MONTH);
		if (date != today) {
			for (int i = 0; i < 24; i++) {
				hourinfos[i] = hourinfos[24 + i];
				hourinfos[24 + i] = new HourInfo();
			}
			today = date;
		}
	}

	public static synchronized void putReg(String os, long uid, String nick) {
		Calendar cld = Calendar.getInstance();
		RegItem item = new RegItem(uid, nick, cld.getTime());
		if (!last10Stat.offer(item)) {
			last10Stat.poll();
			last10Stat.offer(item);
		}

		initHourinfo(cld);
		int hour = cld.get(Calendar.HOUR_OF_DAY);
		if ("i".equals(os)) {
			hourinfos[24 + hour].iRegcnt += 1;
		} else {
			hourinfos[24 + hour].aRegcnt += 1;
		}
	}

	public static synchronized void putPay(PayType paytype, int money) {
		Calendar cld = Calendar.getInstance();
		initHourinfo(cld);
		int hour = cld.get(Calendar.HOUR_OF_DAY);
		if (paytype == PayType.IAP) {
			hourinfos[24 + hour].iPaycnt += 1;
			hourinfos[24 + hour].iMoneysum += money;
		} else {
			hourinfos[24 + hour].aPaycnt += 1;
			hourinfos[24 + hour].aMoneysum += money;
		}
	}

	public static Queue<RegItem> getLast10Stat() {
		return last10Stat;
	}

	public static HourInfo[] getHourinfo() {
		return hourinfos;
	}

}
